package com.itheima.pinda.log.event;

import com.itheima.pinda.log.entity.LoginLogDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.function.Consumer;

/**
 * 异步监听日志事件
 */
@Slf4j
@AllArgsConstructor
public class LoginListener {
    private Consumer<LoginLogDTO> consumer;


    /**
     * 事件被发布后, 就会被这个监听器所监听到
     *
     * @param event
     */
    @Async
    @Order
    @EventListener(LoginEvent.class)
    public void saveSysLog(LoginEvent event) {
        LoginLogDTO loginLogDTO = (LoginLogDTO) event.getSource();
        consumer.accept(loginLogDTO);
    }
}
